<?php
	header('Content-Type: application/json');
	require_once("util.php");
	require_once("sql.php");
	require_once("user.php");
	require_once("user.php");
	require_once("manageuserrole.php");
	require_once("manageruser.php");
	require_once ("userrolemapping.php");
	
	
	$key ="user";
	$user=null;
	if(session_id() == '') {
		session_start();
	}
	$key = "user";
	$user = unserialize ( $_SESSION [$key] );
	$manageuserrole =new ManageUserRole();
	$unitId=$user->getUnitId();
	$canUpdateUser=$user->isActionAllowed("CAN_UPDATE_USER","GLOBAL");
	

?>

<?php 
if($_SERVER['REQUEST_METHOD'] == "GET"){
	
	$companies=array();
	$responseJson="";
	
	//Get the fulldetils of company(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
		$userrolemappings=$manageuserrole->userRoleListAll();
		
		$responseJson .="[";
		
		if(count($userrolemappings)==0){
			$userrolemapping=$userrolemappings[0];
			$responseJson .="{";
			$responseJson .= '"uid":"' .$userrolemapping->getUserId() .'",';
			$responseJson .= '"username":"' .$userrolemapping->getUserName() .'",';
			$responseJson .= '"roleid":"' .$userrolemapping->getRoleId() .'",';
			$responseJson .= '"rolename":"' .$userrolemapping->getRoleName() .'",';
			$responseJson .= '"username":"' .$userrolemapping->getUserName() .'"';
			$responseJson .="}";
				
		}else {
			foreach($userrolemappings as $userrolemapping){
			$responseJson .="{";
			$responseJson .= '"uid":"' .$userrolemapping->getUserId() .'",';
			$responseJson .= '"username":"' .$userrolemapping->getUserName() .'",';
			$responseJson .= '"roleid":"' .$userrolemapping->getRoleId() .'",';
			$responseJson .= '"rolename":"' .$userrolemapping->getRoleName() .'",';
			$responseJson .= '"username":"' .$userrolemapping->getUserName() .'"';
			$responseJson .="},";
			}
			$responseJson=substr($responseJson,0,strlen($responseJson)-1);
		}
		$responseJson .="]"; 
	}else {//for update & delete operation, get the information about the selected user 
		if (array_key_exists("key",$_REQUEST)){

			$userPK= $_REQUEST["key"];
			$roles=$manageuserrole->userRoleListAllSelected($userPK);
			
			$manageusers=new ManageUsers();
			$selectedUser=$manageusers->getUser($userPK);
			$userName=$selectedUser->getUserName();
				
			
			$responseJson='[';
			$responseJson .='{"id":"username","label":"User Name","display":"true","value":"<b>' .$userName .'</b>","type":"label"},';
			$responseJson .='{"id":"user_id","display":"false","value":"' .$userPK .'","type":"input_hidden"},';
				
			
			$roleStrs=array();
			foreach($roles as $role){
				$checkedStr="";
				if($role->isChecked()=="CHECKED"){
					$checkedStr= ',"op_selected":"true"';
				}
				$roleId=$role->getId();
				$roleName=$role->getRole();
				$roleStr='{"op_value":"' .$roleId .'","op_label":"' .$roleName.'" ' .$checkedStr .'}';
				
				array_push($roleStrs,$roleStr);
			}
			$optionStr=implode(",",$roleStrs);
			$responseJson .='{"id":"role_id","label":"Role","display":"true","value":[' .$optionStr .'],"type":"input_select"}';
			$responseJson .=']';
		}
	}
	echo $responseJson;
} elseif ($_SERVER['REQUEST_METHOD'] == "POST"){
	if (array_key_exists("action",$_REQUEST)){
		$action= $_REQUEST["action"];
		
		$success ='{"status" :"success"}';
		$failure ='{"status" :"faiure"}';
		
		if($action=="update"){
			$status=$manageuserrole->userRoleUpdate($_REQUEST["user_id"],$_REQUEST["role_id"]);
			
			if($status){echo $success;}
			else {echo $failure;}
		}else if($action=="delete"){
			$status=$manageuserrole->userRoleRemove($_REQUEST["user_id"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else{
			echo $failure;
		}
		
	}else{
		echo $failure;
	}
}
?>